package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.FileAuthRef;
import com.ruoyi.common.core.domain.vo.UpdateAuthGrantVO;
import com.ruoyi.system.mapper.FileAuthRefMapper;
import com.ruoyi.system.service.FileAuthRefService;
import io.jsonwebtoken.lang.Collections;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
@Slf4j
public class FileAuthRefServiceImpl implements FileAuthRefService {

    private final FileAuthRefMapper fileAuthRefMapper;

//    @Override
//    public List<Map<String, Object>> selectUserAndRoleList() {
//        List<Map<String, Object>> maps = fileAuthRefMapper.selectUserAndRoleList();
//        //将数据转换成前端样式
//        return maps.stream().map(map -> {
//            if ("0".equals(map.get("isHighlighted").toString())){
//                map.put("isHighlighted",false);
//            }else {
//                map.put("isHighlighted",true);
//            }
//            return map;
//        }).collect(Collectors.toList());
//    }

    @Override
    public List<Map<String, Object>> selectUserAndRoleList() {
        return fileAuthRefMapper.selectUserAndRoleList();
    }

    /**
     * 插入权限信息
     * @param fileAuthRefList
     * @return
     */
    @Override
    public boolean insertFileAuthRef(UpdateAuthGrantVO fileAuthRefList) {
        Assert.isTrue(!ObjectUtils.isEmpty(fileAuthRefList.getFileId()),"fileId不能为空");
        Assert.isTrue(!ObjectUtils.isEmpty(fileAuthRefList.getRefId()),"refId不能为空");
        try {
            //删除以前的权限信息
            fileAuthRefMapper.deleteFileAuthRefByFileIdAndRefId(fileAuthRefList.getFileId(),fileAuthRefList.getRefId(),fileAuthRefList.getType());
            if (Collections.isEmpty(fileAuthRefList.getAssignedList())){
                return true;
            }
            List<FileAuthRef> fileAuthRefs = new ArrayList<>();
            fileAuthRefList.getAssignedList().forEach(authId -> {
                FileAuthRef fileAuthRef = new FileAuthRef();
                fileAuthRef.setFileId(fileAuthRefList.getFileId());
                fileAuthRef.setRefId(fileAuthRefList.getRefId());
                fileAuthRef.setType(fileAuthRefList.getType());
                fileAuthRef.setAuthId(authId);
                fileAuthRefs.add(fileAuthRef);
            });
            //插入新的权限信息
            fileAuthRefMapper.batchInsertFileAuthRef(fileAuthRefs);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public List<Long> selectAllByUserIdAndFileId(Long userId, Long fileId,Integer authId) {
        if (!ObjectUtils.isEmpty(userId) && !ObjectUtils.isEmpty(fileId)){
            return fileAuthRefMapper.selectAllByUserIdAndFileId(userId,fileId,authId);
        }
        return new ArrayList<>();
    }


}
